import React from "react";

const Editguest = () => {
    // 接收跳转参数
    const [searchParams] = React.Router.useSearchParams()

    let [id, SetId] = React.useState(searchParams.get('id') ? searchParams.get('id') : 0) 

    // console.log(id)
    let [guest,SetGuest] = React.useState({})

    // 创建一个表单对象
    const [form] = React.UI.Form.useForm()

    React.useEffect(() => {
        GuestData()
    } ,[])


    // 住户详情
    const GuestData = async () => {
        var result = await React.HTTP.post('/guest/info', {id: id, busid: React.Business.id})

        if(result.code == 0){
            React.error(result.msg)
            return false
        }

        SetGuest(result.data)

        // 重置表单数据
        form.setFieldsValue(result.data)
    }


    // 编辑
    const edit = async (values:any) => {
        values.id = id
        values.busid = React.Business.id

        var result = await React.HTTP.post('/guest/edit',values)

        if(result.code == 0){
            React.error(result.msg,() => {})
            return false
        }else{
            React.success(result.msg)
            return false
        }
    }


    return (
        <>
            <React.UI.NavBar back='返回' onBack={() => { React.navigate(-1) }}>
                编辑入住人
            </React.UI.NavBar>

            <React.UI.Form
                form={form}
                initialValues={guest}
                name='form'
                layout='horizontal'
                mode='card'
                onFinish={edit}
                footer={
                    <React.UI.Button block type='submit' color='primary' size='large'>
                        提交
                    </React.UI.Button>
                }
            >

                <React.UI.Form.Item name="nickname" label="入住人姓名" rules={[{ required: true }]}>
                    <React.UI.Input placeholder="请输入入住人姓名" />
                </React.UI.Form.Item>

                <React.UI.Form.Item name="mobile" label="手机号" rules={[{ required: true }, { pattern: /^1[3456789]\d{9}$/, message: '手机号格式不对' }]}>
                    <React.UI.Input placeholder="请输入手机号" />
                </React.UI.Form.Item>

                <React.UI.Form.Item name="sex" label="性别" rules={[{ required: true }]}>
                    <React.UI.Radio.Group>
                        <React.UI.Space direction='vertical'>
                            <React.UI.Radio value='1'>男</React.UI.Radio>
                            <React.UI.Radio value='2'>女</React.UI.Radio>
                        </React.UI.Space>
                    </React.UI.Radio.Group>
                </React.UI.Form.Item>
            </React.UI.Form>

        </>
    )
}

export default Editguest;